package com.skyline.jfs.storage.server;

import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;

import org.springframework.stereotype.Component;

import com.skyline.jfs.common.config.Config;
import com.skyline.jfs.namespace.dao.JFSDirectoryDao;
import com.skyline.jfs.namespace.dao.JFSFileDao;
import com.skyline.jfs.storage.bean.Loader;
import com.skyline.jfs.storage.bean.Storer;
import com.skyline.jfs.storage.bean.impl.LoaderImpl;
import com.skyline.jfs.storage.bean.impl.StorerImpl;
import com.skyline.jfs.storage.dao.JFSStorageDao;
import com.skyline.jfs.storage.service.Commander;
import com.skyline.jfs.storage.service.impl.CommanderImpl;

@Component("jfsNodeServer")
public class JFSNodeServer {

	public void initalServer(JFSFileDao jfsFileDao, JFSDirectoryDao jfsDirectoryDao,JFSStorageDao jfsStorageDao) {
		try {
			/**
			 * regist the port 10140 for the JFS
			 * */
			LocateRegistry.createRegistry(10140);
			
			/**
			 * new a Storer;
			 */
			Storer storer = new StorerImpl();
			storer.setJfsStorageDao(jfsStorageDao);
			
			/**
			 * bind the storer to the port
			 */
			Naming.rebind("rmi://"+Config.getLocalFSConfig("IP_Address")+":10140/storer", storer);
			
			
			/**
			 * new a Loader;
			 */
			Loader loader =new LoaderImpl();
			loader.setJfsFileDao(jfsFileDao);
			//loader.setJfsStorageDao(jfsStorageDao);
			
			/**
			 * bind the loader to the port
			 */
			Naming.rebind("rmi://"+Config.getLocalFSConfig("IP_Address")+":10140/loader", loader);
			
			/**
			 * new a ClusterInfoService
			 */
			Commander commander = new CommanderImpl();
			commander.setJfsDirectoryDao(jfsDirectoryDao);
			commander.setJfsFileDao(jfsFileDao);
			commander.setJfsStorageDao(jfsStorageDao);
			/**
			 * bind the commander to the port
			 */
			Naming.rebind("rmi://"+Config.getLocalFSConfig("IP_Address")+":10140/commander", commander);
			
			
			System.out.println("======StorerServer has been started!======");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
